package cn.wb.system.service.impl;

import cn.wb.base.service.impl.BaseServiceImpl;
import cn.wb.system.domain.Role;
import cn.wb.system.mapper.MenuMapper;
import cn.wb.system.mapper.PermissionMapper;
import cn.wb.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-01-03
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    // 新增
    @Override
    @Transactional
    public void add(Role role) {
        // 添加角色数据
        super.add(role);
        // 添加 role-menu 中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());
        // 添加role-permission 中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());

    }

    // 编辑
    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);

        // 删除 role-menu 中间表
        menuMapper.deleteByRoleId(role.getId());
        // 添加 role-menu 中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());
        //  删除role-permission 中间表
        permissionMapper.deleteByRoleId(role.getId());
        // 添加role-permission 中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());

    }
}
